/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#include "sle_lite_tool_seek.h"

void OnSleTSeekStateChangeCb(SleSeekState seekState) {
    SLE_TEST_TOOL_LOG("[SleSeekTestTool] OnSleTSeekStateChangeCb state:%s(%hhu)", SleSeekStateStr(seekState),seekState);
}

void OnSleTSeekResultCb(SleSeekResult *result) {
    printf("[SleSeekTestTool] OnSleTSeekResultCb:\r\n");
    char *data = (char*)malloc((result->dataLength + 1) * sizeof(char));
    memcpy(data, result->data, result->dataLength);
    data[result->dataLength] = '\0';
    SLE_TEST_TOOL_LOG("[SleSeekTestTool] ["MACSTR"], dataStatus(%d), rssi(%d), eventType(%d)",
        MAC2STR(result->addr.addr), result->dataStatus, result->rssi, result->eventType);
    free(data);
}

void TStartSeek(void)
{
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .repateFilter = DEFAULT_SLE_SEEK_REPATE_FILTER,
        .filterPolicy = DEFAULT_SLE_SEEK_FILTER_POLICY,
        .phys = DEFAULT_SLE_SEEK_PHYS,
        .type[0] = DEFAULT_SLE_SEEK_TYPE,
        .interval[0] = 15000,
        .window[0] = 1500,
    };
    param.type[0] = (SleSeekType)SleGetInputNumber("Please input seek type(0:passive, 1:active):");
    SleErrorCode ret = StartSeek(&param);
    if(ret != SLE_NO_ERROR) {
        printf("[SleSeekTestTool] Start failed, ret:%hhu\r\n", ret);
    } else {
        SLE_TEST_TOOL_LOG("[SleSeekTestTool] start seek success");
    }
}

void TStopSeek(void)
{
    SleErrorCode ret = StopSeek();
    if(ret != SLE_NO_ERROR) {
        printf("[SleSeekTestTool] Stop failed, ret:%hhu\r\n", ret);
    } else {
        SLE_TEST_TOOL_LOG("[SleSeekTestTool] stop seek success");
    }
}

void TGetDefaultSeekParamConfig(void)
{
    SleSeekParam seekParam;
    SleErrorCode ret = GetDefaultSeekParamConfig(&seekParam);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] TGetDefaultSeekParamConfig failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] addrType:%hhu\r\n", seekParam.addrType);
    printf("[SleHostTestTool] repateFilter:%hhu\r\n", seekParam.repateFilter);
    printf("[SleHostTestTool] filterPolicy:%hhu\r\n", seekParam.filterPolicy);
    printf("[SleHostTestTool] phys:%hhu\r\n", seekParam.phys);
    printf("[SleHostTestTool] type[0]:%hhu\r\n", seekParam.type[0]);
    printf("[SleHostTestTool] interval[0]:%hu\r\n", seekParam.interval[0]);
    printf("[SleHostTestTool] window[0]:%hu\r\n", seekParam.window[0]);
    printf("[SleHostTestTool] type[1]:%hhu\r\n", seekParam.type[1]);
    printf("[SleHostTestTool] interval[1]:%hu\r\n", seekParam.interval[1]);
    printf("[SleHostTestTool] window[1]:%hu\r\n", seekParam.window[1]);
    printf("[SleHostTestTool] type[2]:%hhu\r\n", seekParam.type[2]);
    printf("[SleHostTestTool] interval[2]:%hu\r\n", seekParam.interval[2]);
    printf("[SleHostTestTool] window[2]:%hu\r\n", seekParam.window[2]);
}

void SleSeekToolHalper(void)
{
    printf("******SleSeekTool Command List*******\r\n");
    for (uint16_t i = 0; i < g_seekCmdNum; i++) {
        printf("*     %02d - %-20s     *\r\n", i, g_seekCmdList[i].desc);
    }
    printf("*************************************\r\n");
}

void SleSeekToolCmdHandler(int argc, char **argv)
{
    printf("SleSeekToolCmdHandler in\r\n");
    //试配uart接口的调用方式
    SleResetEnv();
    //从函数名字之后开始处理参数列表
    SleSetBufEnv(argc, argv, 1);
    int index = SleGetInputNumber("Please input cmd index:\r\n");
    if(index < g_seekCmdNum) {
        printf("cmd: %d\r\n", index);
        printf("Execute: %s\r\n", g_seekCmdList[index].desc);
        g_seekCmdList[index].func();
    }
    if(index != 0) g_seekCmdList[0].func();
}

#ifdef __cplusplus
}
#endif // __cplusplus